package org.jazz.board.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.log4j.Logger;

public abstract class BbsAbstractExecutor {
	
	protected Connection conn;
	protected PreparedStatement pstmt;
	protected ResultSet rs;
	
	Logger log = Logger.getLogger(BbsAbstractExecutor.class);
	
	static {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Class.forName("org.apache.commons.dbcp.PoolingDriver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public void doJob() throws Exception {
		try {
			log.info("------------1-----------");
			ready();
			log.info("------------2-----------");
			conn.setAutoCommit(false);
			
			execute();
			log.info("------------3-----------");
			conn.commit();
			log.info("------------4-----------");
			conn.setAutoCommit(true);
			
			/*rs.close();
			rs = null;
			
			pstmt.close();
			pstmt = null;
			
			conn.close();
			conn = null;*/
			
		} catch(Exception e) {
			log.info("------------5-----------");
			e.printStackTrace();			
			conn.rollback();
			throw new Exception(e.getMessage());
			
		} finally {
			log.info("------------6-----------");
			closeAll();
		}
		
	}
	
	private void ready() throws Exception {
		conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:pool");
		
	}
	
	public abstract void execute() throws Exception;
	
	private void closeAll() throws Exception {
		if(rs != null) { try { rs.close(); } catch(Exception e) {;} }
		if(pstmt != null) { try { pstmt.close(); } catch(Exception e) {;} }
		if(conn != null) { try { conn.close(); } catch(Exception e) {;} }
	}
	
}